﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "AddModifyUCSDlg.h"
#include "ui_UCSAddModifyDlg.h"
#include "GbmpNew.h"
#include "Vector3dUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

GbmpUCSAddModifyDlg::GbmpUCSAddModifyDlg(const std::wstring& sName, const gcmp::Coordinate3d &oCoord, QWidget *parent /*= 0*/):QDialog(parent, Qt::Dialog), m_oCoord(oCoord), m_sName(sName)
{
    m_opUi = NEW_AS_OWNER_PTR(Ui::UCSAddModifyDlg);
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opUi != nullptr, L"m_opUi为空",L"GDMPLab",L"2024-03-30");
    m_opUi->setupUi(this);

    this->connect(m_opUi->okButton, SIGNAL(clicked()), this, SLOT(OnOkBtn()));
    this->connect(m_opUi->cancelButton, SIGNAL(clicked()), this, SLOT(OnCancelBtn()));

    m_opUi->lineEditName->setText(QString::fromStdWString(sName));
    m_opUi->lineEditOriginX->setText(QString::number(oCoord.GetOrigin().X()));
    m_opUi->lineEditOriginY->setText(QString::number(oCoord.GetOrigin().Y()));
    m_opUi->lineEditOriginZ->setText(QString::number(oCoord.GetOrigin().Z()));
    m_opUi->lineEditXAxisX->setText(QString::number(oCoord.GetX().X()));
    m_opUi->lineEditXAxisY->setText(QString::number(oCoord.GetX().Y()));
    m_opUi->lineEditXAxisZ->setText(QString::number(oCoord.GetX().Z()));
    m_opUi->lineEditYAxisX->setText(QString::number(oCoord.GetY().X()));
    m_opUi->lineEditYAxisY->setText(QString::number(oCoord.GetY().Y()));
    m_opUi->lineEditYAxisZ->setText(QString::number(oCoord.GetY().Z()));
}

GbmpUCSAddModifyDlg::~GbmpUCSAddModifyDlg()
{

}

void GbmpUCSAddModifyDlg::OnOkBtn()
{
    m_sName = m_opUi->lineEditName->displayText().toStdWString();
    gcmp::Vector3d ptOrigin;
    ptOrigin.SetX(m_opUi->lineEditOriginX->displayText().toDouble());
    ptOrigin.SetY(m_opUi->lineEditOriginY->displayText().toDouble());
    ptOrigin.SetZ(m_opUi->lineEditOriginZ->displayText().toDouble());
    gcmp::Vector3d xAxis, yAxis;
    xAxis.SetX(m_opUi->lineEditXAxisX->displayText().toDouble());
    xAxis.SetY(m_opUi->lineEditXAxisY->displayText().toDouble());
    xAxis.SetZ(m_opUi->lineEditXAxisZ->displayText().toDouble());
    yAxis.SetX(m_opUi->lineEditYAxisX->displayText().toDouble());
    yAxis.SetY(m_opUi->lineEditYAxisY->displayText().toDouble());
    yAxis.SetZ(m_opUi->lineEditYAxisZ->displayText().toDouble());
    if (!xAxis.IsValid() || xAxis.IsZero() || !yAxis.IsValid() || yAxis.IsZero())
    {
        return;
    }
    xAxis.Normalize();
    yAxis.Normalize();
    if (!gcmp::Vector3dUtils::IsPerpendicular(xAxis, yAxis))
    {
        gcmp::Vector3d zAxis = xAxis.Cross(yAxis);
        zAxis.Normalize();
        yAxis = zAxis.Cross(xAxis);
    }
    m_oCoord = gcmp::Coordinate3d(ptOrigin, xAxis, yAxis);
    QDialog::accept();
}

void GbmpUCSAddModifyDlg::OnCancelBtn()
{
    QDialog::reject();
}

const std::wstring& GbmpUCSAddModifyDlg::GetName() const
{
    return m_sName;
}

const gcmp::Coordinate3d& GbmpUCSAddModifyDlg::GetCoordinate() const
{
    return m_oCoord;
}
